<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-02-22
 * Time: 15:57:05
 * Info: 角色组控制器
 */

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\AuthGroup as AuthGroupModel;
use think\facade\View;

class AuthGroup extends Base
{

    /**
     * 角色组列表
     * @return \support\Response|\think\response\Json|\think\response\View
     * @throws \think\Exception
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = (int)input('limit', 10);
            $page  = (int)input('page', 1);
            $first = ($page - 1) * $limit;
            $field = input('field', 'id');
            $order = input('order', 'asc');
            $list  = AuthGroupModel::order("{$field} {$order}")->limit($first, $limit)->select()->toArray();
            $count = AuthGroupModel::count();
            $this->success('ok', $list, $count);
        }

        return View::fetch();
    }

    /**
     * 添加角色组
     *
     *
     * @return \support\Response|\think\response\Json|\think\response\View
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = input('post.');
            if (empty($param['name'])) {
                $this->error("角色名称不能为空");
            }
            if ( ! empty($param['node'])) {
                $param['rules'] = implode(',', $param['node']);
            } else {
                $param['rules'] = "";
            }
            $param['status']      = $param['status'] == 1 ? 'normal' : 0;
            $param['create_time'] = time();
            $param['update_time'] = time();

            AuthGroupModel::create($param);
            $this->success("保存成功");
        }

        return View::fetch();
    }

    /**
     * 编辑角色组
     *
     *
     * @return \support\Response|\think\response\Json|\think\response\View
     * @throws \think\Exception
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = input('post.');

            if (empty($param['id'])) {
                $this->error("参数错误");
            }
            if (empty($param['name'])) {
                $this->error("角色名称不能为空");
            }
            $param['id'] = (int)$param['id'];
            if ($param['id'] == 1) {
                $this->error("不允许修改id为1的数据哦");
            }
            $param['status']      = $param['status'] == 1 ? 'normal' : 0;
            $param['update_time'] = time();

            if ( ! empty($param['node'])) {
                $param['rules'] = implode(',', $param['node']);
            } else {
                $param['rules'] = "";
            }

            AuthGroupModel::update($param, ['id' => $param['id']]);
            $this->success("保存成功");
        }
        $id = (int)input('get.id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        if ($id == 1) {
            $this->error('不允许修改id为1的数据哦');
        }
        $data = AuthGroupModel::where(['id' => $id])->find();
        if ( ! $data) {
            $this->error('获取数据失败');
        }

        View::assign('id', $id);
        View::assign('data', $data);

        return View::fetch();
    }

    /**
     * 删除角色组
     * @return \support\Response|\think\response\Json|\think\response\View
     * @throws \think\Exception
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            $id = (int)input('get.id');
            if (empty($id)) {
                return_error("参数错误");
            }
            if ($id == 1) {
                return_error("不允许删除id为1的数据哦");
            }
            AuthGroupModel::destroy(['id' => $id]);
            $this->success("删除成功");
        }
    }

    /**
     * 获取权限节点
     * @return json string
     */
    public function authorize()
    {
        if ($this->request->isAjax()) {
            $id  = (int)input('get.id');
            $row = AuthGroupModel::where(['id' => $id])->find();
            if (empty($row)) {
                $this->error('数据不存在');
            }
            $list = AuthGroupModel::getAuthorizeNodeListByAdminId($row['rules']);

            $this->success("ok", $list);
        }
    }

}
